package com.pug.web.hotel;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pug.annotation.EnableNoToken;
import com.pug.bo.HotelCommentBo;
import com.pug.pojo.KssUser;
import com.pug.service.hotel.HotelCommentServiceImpl;
import com.pug.service.user.UserServiceImpl;
import com.pug.threadlocal.UserThreadLocal;
import com.pug.vo.HotelCommentPageVo;
import com.pug.vo.HotelCommentVo;
import com.pug.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Hashtable;

/**
 * Author: lqm
 * create: 2022/3/23 13:33
 * Lover: lqx
 *
 * @see
 */
@RestController
public class HotelCommentController extends BaseController {
    @Resource
    UserServiceImpl userService;
    @Resource
    HotelCommentServiceImpl hotelCommentService;
    @Resource(name = "stringRedisTemplate")
    StringRedisTemplate redisTemplate;
    @Resource
    RedisTemplate<String,Integer> integerRedisTemplate;
    //用户评论保存
    @PostMapping("/hotel/comment/save")
    public HotelCommentBo saveComment(@RequestBody HotelCommentVo hotelCommentVo){
        //拿到userId  这个地方要着重关注，在这为啥kssUser拿不到
        KssUser kssUser = UserThreadLocal.get();
//        KssUser kssUser = userService.getById(1506532557851463681L); //先写死
         return hotelCommentService.saveComment(hotelCommentVo, kssUser);
//         return null;
    }
    //加载评论信息
    @PostMapping("/hotel/comment/load")
    public Page<HotelCommentBo> loadHotelComment(@RequestBody HotelCommentPageVo hotelCommentPageVo) {
        return hotelCommentService.loadHotelComment(hotelCommentPageVo);
    }
    //用户回复保存
    //删除评论
    @PostMapping("/hotel/comment/del/{hotelId}/{commentId}")
    public boolean deleteComment(@PathVariable("hotelId")Long hotelId,@PathVariable("commentId") Long commentId){
        KssUser kssUser = UserThreadLocal.get();
        Long userId=kssUser.getId();
//        Long userId=1506532557851463681L;
        return hotelCommentService.deleteComment(userId,commentId);
    }
    //评论点赞
//    @EnableNoToken(true)
    @PostMapping("/hotel/comment/like/{commentId}")
    public int  like(@PathVariable("commentId") Long commentId){
        //拿到用户信息
        KssUser kssUser = UserThreadLocal.get();
        Object o = integerRedisTemplate.opsForHash().get(commentId + "", kssUser.getId() + "");
//        这个地方得加一把锁
        synchronized (this){
            if(o==null){
                integerRedisTemplate.opsForHash().put(commentId+"",kssUser.getId()+"",1);
                return 1;
            }
            else{
                integerRedisTemplate.opsForHash().delete(commentId+"",kssUser.getId());
                return 0;
            }
        }
    }
    //撤销评论点赞
    @PostMapping("/hotel/comment/unlike/{commentId}")
    public int  unlike(@PathVariable("commentId") Long commentId){
        //拿到用户信息
        KssUser kssUser = UserThreadLocal.get();
        Object o = integerRedisTemplate.opsForHash().get(commentId + "", kssUser.getId() + "");
        if(o==null){
            integerRedisTemplate.opsForHash().put(commentId+"",kssUser.getId()+"",1);
            return 1;
        }
        else{
            integerRedisTemplate.opsForHash().delete(commentId+"",kssUser.getId()+"");
            return 0;
        }
    }
}
